/*
** ADOBE SYSTEMS INCORPORATED
** Copyright 2012 Adobe Systems Incorporated
** All Rights Reserved.
**
** NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the
** terms of the Adobe license agreement accompanying it.  If you have received this file from a
** source other than Adobe, then your use, modification, or distribution of it requires the prior
** written permission of Adobe.
*/

#include "Missile.h"
#include "Game.h"
#include "Ship.h"
#include <MathUtils.h>

namespace SpaceTriangle
{
	Missile::Missile(flash::display3D::Context3D _ctx3d, Game *_game, vec3 _pos, vec3 _speed, float _rads) :
		Actor(_ctx3d),
		game(_game),
		rads(_rads)
	{
		speed = _speed;
		pos = _pos;

		vec3 x(0,2,0);
		x.rotate2D(rads);
		speed = x;

		i3dbuffer = ctx3d->createIndexBuffer(6);
	    int indicies[] = {
	    	2,1,0,2,0,3
	    };
	    i3dbuffer->uploadFromByteArray(internal::get_ram(), (int)&indicies[0], 0, 6, (void*)&indicies[0]);

	    v3dbuffer = ctx3d->createVertexBuffer(4, 3);
	    float verticies[] = {
	    	-2.5, -2.5, 1.0,
	        -2.5, 2.5, 1.0,
	        2.5, 2.5, 1.0,
	        2.5, -2.5, 1.0,
	    };
	    v3dbuffer->uploadFromByteArray(internal::get_ram(), (int)&verticies[0], 0, 4, (void*)&verticies[0]);

	    colorVector = internal::new_Vector_Number();
	    colorVector[0] = internal::new_Number(1.0);
	    colorVector[1] = internal::new_Number(0.5);
	    colorVector[2] = internal::new_Number(0.5);
	    colorVector[3] = internal::new_Number(0.5);

	    objectToWorld = flash::geom::Matrix3D::_new();
	}

	void Missile::tick()
	{
		pos += speed;

		if(pos.x < -game->width / 2.0)
			pos.x = game->width / 2.0;
		else if(pos.x > game->width / 2.0)
			pos.x = -game->width / 2.0;
		
		if(pos.y < -game->height / 2.0)
			pos.y = game->height / 2.0;
		else if(pos.y > game->height / 2.0)
			pos.y = -game->height / 2.0;
	}

	void Missile::render()
	{
		objectToWorld->identity();
		objectToWorld->appendRotation(rads * (180.0/M_PI), flash::geom::Vector3D::Z_AXIS, internal::_null);
		objectToWorld->appendTranslation(pos.x, pos.y, pos.z);

		ctx3d->setProgramConstantsFromMatrix(flash::display3D::Context3DProgramType::VERTEX, 4, objectToWorld, true);
		ctx3d->setProgramConstantsFromVector(flash::display3D::Context3DProgramType::FRAGMENT, 0, colorVector, -1);
		ctx3d->setVertexBufferAt(0, v3dbuffer, 0, flash::display3D::Context3DVertexBufferFormat::FLOAT_3);
		ctx3d->drawTriangles(i3dbuffer, 0, -1);
	}
}